package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Project;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class ProjectDao {
	private DBHelp<Project> db=new DBHelp<Project>();
	
	public List<Project> findByUserId(String id){
		final String sql="SELECT * FROM t_project WHERE id IN (SELECT t_project_id FROM t_user_project WHERE t_user_id =?) ORDER BY createtime DESC";
		return db.executeQueryForList(sql, new ProjectRowMapper(), id);
	}

	private class ProjectRowMapper implements RowMapper<Project>{

		public Project mapperRow(ResultSet rs) throws SQLException {
			Project project=new Project();
			project.setId(rs.getString("id"));
			project.setName(rs.getString("name"));
			project.setDesc(rs.getString("desc"));
			project.setCreatetime(rs.getString("createtime"));
			return project;
		}

	}
	
	public void save(Project p){
		final String sql="insert into t_project(id,`name`,`desc`,createtime)values(?,?,?,?)";
		 db.executeSQL(sql, p.getId(),p.getName(),p.getDesc(),p.getCreatetime());
		
	}

	public Project findById(String projectid) {
		String sql = "select * from t_project where id=?";
		return db.executeQueryForObject(sql, new ProjectRowMapper(), projectid);
	}

}
